
"""
    pycoopiler - a compiler developed to PCS2056 course (Compilers) at Poli-USP
    Copyright (C) 2008 Andre Felipe Santos    
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ---
        
    symbtable:
    This class implements a symbol table to the compiler. It raises an exception if you 
    try to add a symbol that already exists or if you try to get a symbol that does not
    exist
    
"""

from pycoopiler.enum import enum

class symbtable():
    """A simple symbol table class.
    It's a Python dictionary, with the key being the symbol name to
    the programmer. The key related value is a 3-uple of:
    	the assembler name
    	the type
    	the number
    The symbol table also has an attribute, the scope to which it
    refers."""
    
    #types = enum("LABEL VAR")
    
    def __init__ (self, scope):
        self.__table = {}
        self.__scope = scope   
    
    #def __init__ (self, parent):
    #    self.__table = []
    #    self.__parent = parent
    
    #Symbol: id (do programador)
    #Value: label (do montador) e valor
    def put(self, symbol):
        if symbol in self.__table:
            raise Exception('Symbol already in table')
        else:
            number = len(self.__table)
            name = "VAR" + str(number)
            self.__table[symbol] = name, number
            #return self.__table.index(symbol)
    
    #retorna a posição      
    def get(self, symbol):
        if symbol in self.__table:
            return self.__table[symbol][1]
        else:
            raise Exception('Non existant symbol')
            
    #retorna nome
    def get_name(self, symbol):
    	if symbol in self.__table:
    		return self.__table[symbol][0]
    	else:
    		raise Exception('Non existant symbol')
    		
    
    def get_table(self):
        return self.__table
    
    def clear(self):
        self.__table = {}
    

        
    
    
    
